/******************************************/
/* OUTFLOWS AGE OUTFLOWS AGE OUTFLOWS AGE */
/******************************************/
clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"
cd "ImmPanelRevis19\Donnees\select"

/***************/
/* Estimations */
/***************/
capture program drop regsamp
program define regsamp
version 11
syntax, occp(name) outc(name)

use final.dta, clear

gen reg=substr(ze,1,2)
gen nreg="11" if reg=="11" | reg=="00"
replace nreg="24" if reg=="24"
replace nreg="27" if reg=="26" | reg=="43"
replace nreg="28" if reg=="23" | reg=="25"
replace nreg="32" if reg=="31" | reg=="22"
replace nreg="44" if reg=="42" | reg=="21" | reg=="41"
replace nreg="52" if reg=="52" 
replace nreg="53" if reg=="53" 
replace nreg="75" if reg=="72" | reg=="74" | reg=="54"
replace nreg="76" if reg=="91" | reg=="73"
replace nreg="84" if reg=="83" | reg=="82"
replace nreg="93" if reg=="93" 
replace nreg="94" if reg=="94" 

xi , pre(REG_) i.nreg

gen occ="bcq" if bcq==1
replace occ="cad" if cad==1

/* create some outcome variables */
gen ldp=ln(dp/l.dp)
gen dftfy=(dp==360)-(L.dp==360)
gen oshift=(`occp'==0 & L.`occp'==1)
replace oshift=( (cad==0 & L.cad==1) | (tecemp2==0 & L.tecemp2==1) | (bcq==0 & L.bcq==1) | (bcnq==0 & L.bcnq==1)) if "`occp'"=="all"

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd
/* residual outcome */
quietly: reg `outc' F_*  if btime==2
predict r`outc' if btime==2, residuals

/* rank in the wage distribution */
drop F_*
egen qinit = fastxtile(rlsnd) if ftreat, by(ze an btime) nq(100)
replace qinit=qinit/100

/* interaction term and instruments */
gen lqinit=L.qinit
gen inter= dimm*L.qinit
gen linter=ldimm*L.qinit
gen inter75= dimm75 * L.qinit
gen linter75=ldimm75*L.qinit
gen interm2= dimtm2 * L.qinit
gen linterm2=ldimtm2 *L.qinit

/* create quartiles */
gen age2534 =(age>=25 & age<=34) if btime==1
gen age3544 =(age>=35 & age<=44) if btime==1
gen age4550 =(age>=45 & age<=50) if btime==1
replace age2534=L.age2534 if btime==2
replace age3544=L.age3544 if btime==2
replace age4550=L.age4550 if btime==2

foreach OCC in age2534 age3544 age4550{
gen dimm`OCC'=dimm*`OCC'
gen inter`OCC'= dimm*L.qinit*`OCC'
gen dimm75`OCC'=dimm75*`OCC'
gen inter75`OCC'=dimm75*L.qinit*`OCC'
}

keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge
ivreg2 r`outc' (dimm dimmage3544 dimmage4550= dimm75 dimm75age3544 dimm75age4550) Y_*  REG_* [aweight = iw2], partial(Y_* REG_*) cl(ze)
estimates store AG`occp'iv

end

capture program drop mobtyp
program define mobtyp
version 11
syntax, outc(name) file(name) 

/* OUTFLOWS */
regsamp, occp(all) outc(`outc')
regsamp, occp(cad) outc(`outc')
regsamp, occp(tecemp2) outc(`outc')
regsamp, occp(bcq) outc(`outc')
regsamp, occp(bcnq) outc(`outc')
/*
regsamp, occp(trad) outc(`outc')
regsamp, occp(ntrad) outc(`outc')
regsamp, occp(constbc) outc(`outc')
*/

 /* IV */
esttab AGalliv  AGcadiv AGtecemp2iv AGbcqiv AGbcnqiv /* bciv tradiv ntradiv constbciv */ ///
using "C:\Users\Public\Documents\Tab21\\`file'.rtf" /// 
, append title("s")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

end
capture rm "C:\Users\Public\Documents\Tab21\TableA10_age.rtf"
mobtyp , outc(lshift) file(TableA10_age)


/**********************************/
/* regression de salaire avec AGE */
/**********************************/
capture program drop wagesage
program define wagesage
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

gen age2534 =(age>=25 & age<=34) if btime==1
gen age3544 =(age>=35 & age<=44) if btime==1
gen age4550 =(age>=45 & age<=50) if btime==1
replace age2534=L.age2534 if btime==2
replace age3544=L.age3544 if btime==2
replace age4550=L.age4550 if btime==2

foreach OCC in age2534 age3544 age4550{
gen dimm`OCC'=dimm*`OCC'
gen dimm75`OCC'=dimm75*`OCC'
}

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsn=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsn F_* 
predict rlsn, residuals 
/* change in residual wages */
gen drlsn=D.rlsn 

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsn */
quietly: sum drlsn if an==`year', d
replace drlsn=. if drlsn>`r(p99)' & an==`year'
replace drlsn=. if drlsn<`r(p1)' & an==`year'
}
drop if missing(drlsn)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
gen dummy=1
collapse (sum) nbze = dummy (mean) drlsn dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsn dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsn (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

use temp1.dta, clear
/* IV */
ivreg2 drlsn (dimm dimmage3544 dimmage4550 = dimm75 dimm75age3544 dimm75age4550 ) Y_* [aweight = iw2] , partial(Y_* ) cl(ze)
estimates store agbs`occp'iv

end

wagesage, occp(all) 
wagesage, occp(cad) 
wagesage, occp(tecemp2) 
wagesage, occp(bc) 
wagesage, occp(bcnq) 

/* Age effect RESIDUAL DAILY WAGE */
 esttab agbsalliv  agbscadiv agbstecemp2iv agbsbciv agbsbcnqiv ///
using "C:\Users\Public\Documents\Tab21\TableA10_age.rtf" /// 
, append title("annual wages 2SLS weighted")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
 
/************************************************/
/* regression de annual days worked DP avec AGE */
/************************************************/
capture program drop dpage
program define dpage
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

gen age2534 =(age>=25 & age<=34) if btime==1
gen age3544 =(age>=35 & age<=44) if btime==1
gen age4550 =(age>=45 & age<=50) if btime==1
replace age2534=L.age2534 if btime==2
replace age3544=L.age3544 if btime==2
replace age4550=L.age4550 if btime==2

foreach OCC in age2534 age3544 age4550{
gen dimm`OCC'=dimm*`OCC'
gen dimm75`OCC'=dimm75*`OCC'
}

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsn=ln(dp)
/* residual wages for each year */
quietly: reg lsn F_* 
predict rlsn, residuals 
/* change in residual wages */
gen drlsn=D.rlsn 

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsn */
quietly: sum drlsn if an==`year', d
replace drlsn=. if drlsn>`r(p99)' & an==`year'
replace drlsn=. if drlsn<`r(p1)' & an==`year'
}
drop if missing(drlsn)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

/* IV */
ivreg2 drlsn (dimm dimmage3544 dimmage4550 = dimm75 dimm75age3544 dimm75age4550 ) Y_* [aweight = iw2] , partial(Y_* ) cl(ze)
estimates store agbs`occp'iv

end

dpage, occp(all) 
dpage, occp(cad) 
dpage, occp(tecemp2) 
dpage, occp(bc) 
dpage, occp(bcnq) 

/* Age effect */
 esttab agbsalliv  agbscadiv agbstecemp2iv agbsbciv agbsbcnqiv ///
using "C:\Users\Public\Documents\Tab21\TableA10_age.rtf" /// 
, append title("dp baseline with age interactions")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
  